<template>
  <div class="pagination-container">
    <!-- VitePress 风格的 features 模块 -->
    <div class="vp-features">
      <div v-for="(feature, index) in currentPageData" :key="index" class="vp-feature">
        <a v-if="feature.link" :href="feature.link" class="vp-feature-link">
          <div v-if="feature.icon" class="vp-feature-icon">
            <img
              :src="typeof feature.icon === 'object' ? (isDarkMode ? feature.icon.dark : feature.icon.light) : feature.icon"
              :alt="feature.title"
              class="vp-feature-img"
            >
          </div>
          <h3 class="vp-feature-title">{{ feature.title }}</h3>
          <p class="vp-feature-desc">{{ feature.details }}</p>
        </a>
        <div v-else class="vp-feature-content">
          <div v-if="feature.icon" class="vp-feature-icon">
            <img
              :src="typeof feature.icon === 'object' ? (isDarkMode ? feature.icon.dark : feature.icon.light) : feature.icon"
              :alt="feature.title"
              class="vp-feature-img"
            >
          </div>
          <h3 class="vp-feature-title">{{ feature.title }}</h3>
          <p class="vp-feature-desc">{{ feature.details }}</p>
        </div>
      </div>
    </div>
    
    <!-- 分页控制 -->
    <div class="pagination-controls">
      <button 
        class="pagination-button" 
        :disabled="currentPage === 1"
        @click="goToPage(currentPage - 1)"
      >
        上一页
      </button>
      
      <button 
        v-for="page in pageNumbers" 
        :key="page"
        class="pagination-button"
        :class="{ active: page === currentPage }"
        @click="goToPage(page)"
      >
        {{ page }}
      </button>
      
      <button 
        class="pagination-button" 
        :disabled="currentPage === totalPages"
        @click="goToPage(currentPage + 1)"
      >
        下一页
      </button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, inject } from 'vue';
import { useData } from 'vitepress';

// 使用VitePress的dark mode状态
const { isDark } = useData();
const isDarkMode = computed(() => isDark.value);

// 定义props类型
interface Feature {
  title: string;
  details: string;
  icon?: string | { dark: string; light: string };
  link?: string;
}

// 定义组件props
const props = defineProps<{
  items?: Feature[];
  pageSize?: number;
}>();

// 默认数据 - 20个功能分组，包含icon和link
const defaultFeatures: Feature[] = Array.from({ length: 20 }, (_, i) => ({
  title: `功能分组 ${i + 1}`,
  details: `这是功能分组${i + 1}的详细描述，包含多个相关子功能`,
  icon: '/svg/docker20240407.svg',
  link: `/docker/index${i + 1}`
}));

// 使用传入的items或默认数据
const features = ref<Feature[]>(props.items || defaultFeatures);
const pageSize = ref<number>(props.pageSize || 5);
const currentPage = ref<number>(1);

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(features.value.length / pageSize.value);
});

// 计算当前页的数据
const currentPageData = computed(() => {
  const startIndex = (currentPage.value - 1) * pageSize.value;
  const endIndex = startIndex + pageSize.value;
  return features.value.slice(startIndex, endIndex);
});

// 计算显示的页码
const pageNumbers = computed(() => {
  const numbers: number[] = [];
  for (let i = 1; i <= totalPages.value; i++) {
    numbers.push(i);
  }
  return numbers;
});

// 跳转到指定页
function goToPage(page: number) {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page;
  }
}
</script>

<style scoped>
.pagination-container {
  margin: 0 auto;
  padding: 2rem 0;
}

/* VitePress 风格的 features 样式 - 与默认主题保持一致 */
.vp-features {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 32px;
  margin: 32px 0;
}

.vp-feature {
  background-color: var(--vp-c-bg-soft);
  border-radius: 12px;
  border: 1px solid var(--vp-c-divider);
  padding: 32px 24px;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.vp-feature-link {
  display: block;
  text-decoration: none;
  color: inherit;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.vp-feature-content {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.vp-feature-icon {
  margin-bottom: 16px;
}

.vp-feature-img {
  width: 48px;
  height: 48px;
  object-fit: contain;
}

.vp-feature-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--vp-c-text-1);
  margin: 0 0 12px 0;
  line-height: 1.4;
}

.vp-feature-desc {
  font-size: 14px;
  color: var(--vp-c-text-2);
  margin: 0;
  line-height: 1.6;
  flex: 1;
}

.vp-feature:hover {
  transform: translateY(-2px);
}

/* 分页控制样式 */
.pagination-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--vp-c-divider);
}

.pagination-button {
  padding: 8px 16px;
  border: 1px solid var(--vp-c-divider);
  background-color: var(--vp-c-bg);
  color: var(--vp-c-text-1);
  font-size: 14px;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 40px;
  text-align: center;
}

.pagination-button:hover:not(:disabled) {
  background-color: var(--vp-c-bg-soft);
  border-color: var(--vp-c-text-3);
  color: var(--vp-c-text-1);
}

.pagination-button.active {
  background-color: var(--vp-c-brand);
  color: var(--vp-c-white);
  border-color: var(--vp-c-brand);
}

.pagination-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background-color: var(--vp-c-bg-soft);
  color: var(--vp-c-text-3);
  border-color: var(--vp-c-divider);
}

/* 响应式设计 */
@media (max-width: 960px) {
  .pagination-container {
    padding: 1rem 20px;
  }
  
  .vp-features {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  .pagination-container {
    padding: 1rem 16px;
  }
  
  .vp-feature {
    padding: 24px 16px;
  }
  
  .vp-feature-title {
    font-size: 16px;
  }
  
  .pagination-button {
    padding: 6px 12px;
    font-size: 13px;
  }
}
</style>
